"""
This defines the cmdset for the container. Here we have defined
the commands and the cmdset in the same module, but if you
have many different commands to merge it is often better
to define the cmdset separately, picking and choosing from
among the available commands as to what should be included in the
cmdset - this way you can often re-use the commands too.
"""

from ev import Command, CmdSet

# Some simple commands for the red button

#------------------------------------------------------------
# Commands defined on the container object
#------------------------------------------------------------

class CmdOpenContainer(Command):
    """
    Opens the container object if it has a "lid"

    Usage:
        open <container name>

    """

    key = "open"
    aliases = []
    locks = "cmd:all()"

    def func(self):
        """simply call the right function."""

        caller = self.caller
        
        if not self.args:
            caller.msg("Open what?")
            return
        obj = caller.search(self.args)
        if not obj:
            return
        if caller == obj:
            caller.msg("You can't open yourself.")
            return
        if not obj.access(caller, 'open'):
            if obj.db.get_err_msg:
                caller.msg(obj.db.get_err_msg)
            else:
                caller.msg("You can't open that.")
            return
        
        if self.obj.db.has_lid:
            if self.obj.db.is_open:
                string = "\nThe %s is already open." % self.obj.name
                caller.msg(string)
                return
            string = "\nYou open the %s." % self.obj.name
            caller.msg(string)
            caller.location.msg_contents("%s opens the %s." % (caller.name, self.obj.name), exclude=self.caller)
            # add the relevant cmdsets to button
            self.obj.cmdset.add(ContainerOpenCmdSet)
            # call object method
            self.obj.container_open()
        else:
            string = "\nThe %s cannot be opened." % self.obj.name
            caller.msg(string)

class CmdCloseContainer(Command):
    """
    Closes the container object if it has a "lid"

    Usage:
      close <container name>

    Closes the container.
    """

    key = "close"
    aliases = []
    locks = "cmd:all()"

    def func(self):
        """Close the container"""

        caller = self.caller
        
        if not self.args:
            caller.msg("Close what?")
            return
        obj = caller.search(self.args)
        if not obj:
            return
        if caller == obj:
            caller.msg("You can't close yourself.")
            return
        if not obj.access(caller, 'close'):
            if obj.db.get_err_msg:
                caller.msg(obj.db.get_err_msg)
            else:
                caller.msg("You can't close that.")
            return
        
        if self.obj.db.has_lid:
            if self.obj.db.is_closed:
                string = "\nThe %s is already closed." % self.obj.name
                caller.msg(string)
                return
            string = "\nYou close the %s." % self.obj.name
            caller.msg(string)
            caller.location.msg_contents("%s closes the %s." % (caller.name, self.obj.name), exclude=self.caller)
            # add the relevant cmdsets to button
            self.obj.cmdset.add(ContainerClosedCmdSet)
            # call object method
            self.obj.container_close()
        else:
            string = "\nThe %s cannot be closed." % self.obj.name
            caller.msg(string)
        
#---------------------------------------------------------------
# Command sets for the container object
#---------------------------------------------------------------

# We next tuck these commands into their respective command sets.
# (note that we are overdoing the cdmset separation a bit here
# to show how it works).

class DefaultCmdSet(CmdSet):
    """
    The default cmdset always sits
    on the container object and whereas other
    command sets may be added/merged onto it
    and hide it, removing them will always
    bring it back. It's added to the object
    using obj.cmdset.add_default().
    """
    key = "ContainerDefault"
    mergetype = "Union" # this is default, we don't really need to put it here.

    def at_cmdset_creation(self):
        "Init the cmdset"
        self.add(CmdOpenContainer())
        self.add(CmdCloseContainer())

class ContainerClosedCmdSet(CmdSet):
    """
    This is the command set set on the object when the state
    of the container is closed. It adds the CmdOpenContainer
    commandset so that it can be opened.
    """
    
    key = "ContainerClosedCmdSet"
    # default Union is used *except* if we are adding to a
    # cmdset named LidOpenCmdSet - this one we replace
    # completely.
    #key_mergetype = {"ContainerOpenCmdSet": "Replace"}

    def at_cmdset_creation(self):
        "Populates the cmdset when it is instantiated."
        self.add(CmdOpenContainer())

class ContainerOpenCmdSet(CmdSet):
    """
    This is the command set set on the object when the state
    of the container is open. It adds the CmdClosedContainer
    commandset so that it can be closed.
    """
    
    key = "ContainerOpenCmdSet"
    # default Union is used *except* if we are adding to a
    # cmdset named LidClosedCmdSet - this one we replace
    # completely.
    #key_mergetype = {"ContainerClosedCmdSet": "Replace"}

    def at_cmdset_creation(self):
        "setup the cmdset (just one command)"
        self.add(CmdCloseContainer())